using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Design
{
    public partial class EditorForm : Form
    {
        private string _filename = null;

    //      Private _killAutoEvent As Boolean
    //Public Property KillAutoEvent() As Boolean
    //    Get
    //        If _killAutoEvent = True Then
    //            _killAutoEvent = False
    //            Return True
    //        End If
    //        'Return _killAutoEvent()
    //    End Get
    //    Set(ByVal value As Boolean)
    //        _killAutoEvent = value
    //    End Set
    //End Property

private bool _killAutoEvent = true ;
        public bool KillAutoEvent
        {
            get
            {
                if (_killAutoEvent)
                {
                 _killAutoEvent = false;
                return true;
                }
                 else
                {
                   return false;
                }
            }

            set
            {
                _killAutoEvent = value;
            }

        }

        //public readonly string EditorText
        //{
        //    get
        //    {
        //        return editor.WbBrowser.DocumentText;
        //    }
        //}

        public EditorForm(string initialText)
        {
            InitializeComponent();
            SetText(initialText);
            editor.Tick += new Editor.TickDelegate(editor_Tick);
            CreateHindiBox();
        }

        public EditorForm()
        {
           
        }

        private void SetText(string initialText)
        {
            editor.WbBrowser.DocumentText = initialText;
        }


        private void editor_Tick()
        {
            undoToolStripMenuItem.Enabled = editor.CanUndo();
            redoToolStripMenuItem.Enabled = editor.CanRedo();
            cutToolStripMenuItem.Enabled = editor.CanCut();
            copyToolStripMenuItem.Enabled = editor.CanCopy();
            pasteToolStripMenuItem.Enabled = editor.CanPaste();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _filename = null;
            Text = null;
            editor.BodyHtml = string.Empty;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_filename == null)
            {
                if (!SaveFileDialog())
                    return;
            }
            SaveFile(_filename);
        }

        private bool SaveFileDialog()
        {
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.AddExtension = true;
                dlg.DefaultExt = "htm";
                dlg.Filter = "HTML files (*.html;*.htm)|*.html;*.htm";
                DialogResult res = dlg.ShowDialog(this);
                if (res == DialogResult.OK)
                {
                    _filename = dlg.FileName;
                    return true;
                }
                else
                    return false;
            }
        }

        private void SaveFile(string filename)
        {
            using (StreamWriter writer = File.CreateText(filename))
            {
                writer.Write(editor.DocumentText);
                writer.Close();
            }
        }

        private void LoadFile(string filename)
        {
            using (StreamReader reader = File.OpenText(filename))
            {
                editor.DocumentText = reader.ReadToEnd();
                reader.Close();
                Text = editor.DocumentTitle;
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "HTML files (*.html;*.htm)|*.html;*.htm";
                DialogResult res = dlg.ShowDialog(this);
                if (res == DialogResult.OK)
                {
                    _filename = dlg.FileName;
                }
                else
                    return;
            }
            LoadFile(_filename);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SaveFileDialog())
                SaveFile(_filename);
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SearchDialog dlg = new SearchDialog(editor))
            {
                dlg.ShowDialog(this);
            }
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.SelectAll();
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.Paste();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.Undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.Redo();
        }

        private void textToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, editor.BodyText);
        }

        private void htmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, editor.BodyHtml);
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.Print();
        }

        private void breakToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.InsertBreak();
        }

        private void textToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            using (TextInsertForm form = new TextInsertForm(editor.BodyHtml))
            {
                form.ShowDialog(this);
                if (form.Accepted)
                {
                    editor.BodyHtml = form.HTML;
                }
            }
        }

         public void CreateHindiBox()
         {
        WbHindi.Navigate("D:/Project SimpleCMS/Content/hindi.html");
        WbHindi.ObjectForScripting = new ScriptManager(this);
        Object[] o = new object[1];
        o[0] = "Hi from jscript";
        Object result  = this.WbHindi.Document.InvokeScript("CreateCustomHindiTextArea", o);
         }

         [System.Runtime.InteropServices.ComVisible(true)]
    //<Runtime.InteropServices.ComVisible(True)> _
    public class ScriptManager
      {
       EditorForm _form;
       public ScriptManager(EditorForm form)
       {
           _form = form;
       }

       public void ShowMessage(object obj)
       {
           MessageBox.Show(obj.ToString());
       }

      }


        private void paragraphToolStripMenuItem_Click(object sender, EventArgs e)
        {
            editor.InsertParagraph();
        }

        private void EditorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            editor.StopTimer();
        }

        //hindi
        private void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
       
        }

        //english
        private void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
        //     If KillAutoEvent Then Exit Sub
        //Dim textboxhindi As HtmlElement = WebBrowserHindi.Document.GetElementById("id 1")
        //textboxhindi.Enabled = True
        //textboxhindi.InnerText = EnglishTextBox.Text
        //EnglishTextBox.Enabled = False
        //KillAutoEvent = True

            if (KillAutoEvent) return;
            HtmlElement textboxhindi = WbHindi.Document.GetElementById("id 1");
            textboxhindi.InnerText =editor.WbBrowser.DocumentText ;
            //editor.WbBrowser.;
               // textboxhindi.Enabled = false;
            KillAutoEvent = true;

        }

        private void PanelHindi_Enter(object sender, EventArgs e)
        {
            //if (KillAutoEvent) return;
            //HtmlElement textboxhindi = WbHindi.Document.GetElementById("id 1");
            //textboxhindi.InnerText = editor.WbBrowser.DocumentText;
            ////editor.WbBrowser.;
            ////textboxhindi.Enabled = true;
            //KillAutoEvent = true;
        }

        private void editor_Enter(object sender, EventArgs e)
        {
            //if (KillAutoEvent) return;
            //HtmlElement textboxhindi = WbHindi.Document.GetElementById("id 1");
            // editor.WbBrowser.DocumentText = textboxhindi.InnerText;
            ////editor.WbBrowser.;
            //// textboxhindi.Enabled = false;
            //KillAutoEvent = true;
        }

        private void BtnInsertText_Click(object sender, EventArgs e)
        {
            HtmlElement textboxhindi = WbHindi.Document.GetElementById("id 1");
            editor.WbBrowser.AllowWebBrowserDrop = !editor.WbBrowser.AllowWebBrowserDrop;
            editor.WbBrowser.DocumentText += textboxhindi.InnerText;
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
            
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

      
        
        



    }
}